package thread;

public class SyncDemo1 extends Shop{
    public static void main(String[] args) {
        Shop shop1 = new Shop();
        Shop shop2 = new Shop();

        Thread t1 = new Thread(shop1::buy);
        Thread t2 = new Thread(shop2::buy);
         /*
        Thread t1 = new Thread("小沙"){
            public void run(){
                shop1.buy();
            }
        };
        Thread t2 = new Thread("小金"){
            public void run(){
                shop2.buy();
            }
        };

         */
        t1.start();
        t2.start();


    }
}
class Shop{
    public void buy(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+"正在挑衣服");
            Thread.sleep(3000);
            //不可以用实例化对象（new Object（））作为同步监视器对象，无效
            //同步监视器对象一定是引用类型（123不行）
            //（"123"）虽然可以锁上，但abc并不是合适的对象，比如有多个店，可以同时进行就不需要锁
            //this为锁对象，所的当前shop对象。只要是同一个对象，就一定会被锁住
            synchronized (this){
            System.out.println(t.getName()+"正在试衣服");
            Thread.sleep(3000);
            }
            System.out.println(t.getName()+"结账离开");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
